package com.example.ele_guarantee_hh_pro.comclass;
import com.cloudcc.deveditor.core.CCObject;
import com.cloudcc.deveditor.core.CCService;
import com.cloudcc.deveditor.core.ServiceResult;
import com.cloudcc.deveditor.core.UserInfo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;

/**
 * 资质列表查询
 * @date 2022/1/5
 */
public class BuildCertSearchService {

    private static CCService cs;

    public BuildCertSearchService(UserInfo userInfo){
        cs = new CCService(userInfo);
    }

    public static Map<String, String> getUrl(){
        Map<String, String> map=new HashMap<>();
        List<CCObject> list = cs.cquery("interface_configurat","isstart='1' and environmental_type='elec' and is_deleted='0' and url='/pfbpapi/crm/data/dsk/search/buildCert'");//配置表
        try{
            if(list.size()>0){
                String url=list.get(0).get("url")==null?"":list.get(0).get("url")+"";//地址
                String port=list.get(0).get("port")==null?"":list.get(0).get("port")+"";//地址
                String client_id=list.get(0).get("client_id")==null?"":list.get(0).get("client_id")+"";
                String client_secret=list.get(0).get("client_secret")==null?"":list.get(0).get("client_secret")+"";
                String grant_type=list.get(0).get("grant_type")==null?"":list.get(0).get("grant_type")+"";
                String scope=list.get(0).get("scope")==null?"":list.get(0).get("scope")+"";
                map.put("port", port);
                map.put("url", url);
                map.put("client_id", client_id);
                map.put("client_secret", client_secret);
                map.put("grant_type", grant_type);
                map.put("scope", scope);
            }
        }catch(Exception e){
            e.getMessage();
        }
        return map;
    }
    /**
     * @param url_str 访问路径
     * @param data_str 请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @throws IOException
     * @throws HttpException
     */
    public String sendPost(String data_str){ // 连接cloudcc系统(post)
        java.net.URL urlnet=null;
        HttpURLConnection con =null;
        String sc = "";
        String st = "";
        try {
            Map<String, String> map=getUrl();
            String port = map.get("port");
            String url = map.get("url");
            String url_str=port+url;
            urlnet = new java.net.URL(url_str);
            con=(HttpURLConnection) urlnet.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("access_token",getToken(map));
            con.setRequestProperty("Content-Type","application/json; charset=utf-8");
            con.setConnectTimeout(50000);
            con.setReadTimeout(30000);
            con.connect();
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream(),"UTF-8");
            out.write(data_str);
            out.close();
            //发送文件返回类型
            if (con.getResponseCode() == 200) {
                InputStream is = con.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                while ((sc = br.readLine()) != null) {
                    st = st + sc;
                }
                is.close();
            }
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
            st=e.getMessage();
        }
        //设置协议
        return st;
    }
    public static String sendGet(String url_str){ // 连接cloudcc系统(post)
        java.net.URL url=null;
        HttpURLConnection con =null;
        InputStream urlStream=null;
        BufferedReader reader=null;
        String sc = "";
        String st = "";
        try {
            url = new java.net.URL(url_str);
            con=(HttpURLConnection) url.openConnection();
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.connect();
            //发送文件返回类型
            urlStream = con.getInputStream();
            reader = new BufferedReader(new InputStreamReader(urlStream));
            if (con.getResponseCode() == 200) {
                InputStream is = con.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                while ((sc = br.readLine()) != null) {
                    st = st + sc;
                }
                is.close();
            }
            reader.close();
            urlStream.close();
            con.disconnect();
        } catch (Exception e) {
            e.printStackTrace();

        }
        //设置协议
        return st;
    }
    public static String getToken(Map<String, String> map) {
        String port=map.get("port")+"";
        String client_id=map.get("client_id")+"";
        String client_secret=map.get("client_secret")+"";
        String grant_type=map.get("grant_type")+"";
        String scope=map.get("scope")+"";
        String url=port+"/oauth2/oauth/token?client_id="+client_id
                +"&client_secret="+client_secret+"&scope="+scope+"&grant_type="+grant_type+"";
        String data = sendGet(url);
        String token="";
        String code="";
        JSONObject fromObject =null;
        try {
            fromObject = JSONObject.fromObject(data);
            code=fromObject.getString("code");
            if("200".equals(code)){
                token=fromObject.getJSONObject("data").get("access_token")+"";
            }
            System.out.println("getToken:"+data);
        } catch(Exception e) {
            token=data;
        }
        return token;
    }

}
